incomePage = 1;
$(function() {
	renderIncomePage()
	getTableData()
})

function renderIncomePage() {
	getStatistics()
	renderOverYears()
	renderMonth()
	renderUnitReceive()
	renderVoucherData()
}
const rateparams = {
	username: getUsername,
	xm01: fullTaskNumber,
	status: 2,
	chartPage: 80802
}

function getStatistics() {
	// $.get(`${API_URL}getdata`, {
	// 	...rateparams,
	// 	xm01: fullTaskNumber,
	// 	chartNo: 1
	// }, function(res) {
	// 	if (eval("(" + res + ")").items[0].charts[0].data.length > 0) {
	// 		const re = eval("(" + res + ")").items[0].charts[0].data[0];
	// 		$('.container .wrapper .statistics').html(`<div class="box">
	// 							<div class="left">
	// 								<p>计划到款</p>
	// 								<h3>${re.key01}<span>万元</span></h3>
	// 							</div>
	// 						</div>
	// 						<div class="box">
	// 							<div class="left">
	// 								<p>实际到款</p>
	// 								<h3>${re.key02}<span>万元</span></h3>
	// 							</div>
	// 						</div>
	// 						<div class="box">
	// 							<div class="left">
	// 								<p>执行率</p>
	// 								<h3>${re.key03}%<span></span></h3>
	// 							</div>
	// 							<div class="pie">
	// 								<svg><circle cx="20" cy="20" r="16" stroke="#0FC6C2"  stroke-width="8" fill="rgba(255, 255, 255, 0.2)" stroke-linecap="round" /><circle cx="20" cy="20" r="16"  stroke="${ re.key03>100?'red':'#E5E6EB'}" stroke-width="8" fill="none" stroke-dasharray="${Math.PI * 32}" stroke-dashoffset="${Math.PI * 32 * ( re.key03>100?0: re.key03/100)}"  /></svg>
	// 							</div>
	// 						</div>`)
	// 	}

	// })
	const re = {
		"key01": "302439.9",
		"key02": "155299.02",
		"key03": "51.35"
	}
	$('.container .wrapper .statistics').html(`<div class="box">
						<div class="left">
							<p>计划到款</p>
							<h3>${re.key01}<span>万元</span></h3>
						</div>
					</div>
					<div class="box">
						<div class="left">
							<p>实际到款</p>
							<h3>${re.key02}<span>万元</span></h3>
						</div>
					</div>
					<div class="box">
						<div class="left">
							<p>执行率</p>
							<h3>${re.key03}%<span></span></h3>
						</div>
						<div class="pie">
							<svg><circle cx="20" cy="20" r="16" stroke="#0FC6C2"  stroke-width="8" fill="rgba(255, 255, 255, 0.2)" stroke-linecap="round" /><circle cx="20" cy="20" r="16"  stroke="${ re.key03>100?'red':'#E5E6EB'}" stroke-width="8" fill="none" stroke-dasharray="${Math.PI * 32}" stroke-dashoffset="${Math.PI * 32 * ( re.key03>100?0: re.key03/100)}"  /></svg>
						</div>
					</div>`)
}

function renderOverYears() {
	// $.get(`${API_URL}getdata`, {
	// 	...rateparams,
	// 	xm01: fullTaskNumber,
	// 	chartNo: 2
	// }, function(res) {
	// 	const resp = eval("(" + res + ")").items[0].charts[0];
	// 	const name = ['财务到款'];
	// 	renderLineChart('chart1', resp, name, '年')
	// 	renderBarLineChart('chart5', resp, name)
	// 	renderBarLineChart('chart6', resp, name)
	// })
	
	const resp = {"chart_no":"2","chart_name":"收入-历年财务到款{传参：xm01任务编号；返回值：key01-年份；key02-金额}","key_count":"2","data":[{"key01":"2022","key02":"389.05"},{"key01":"2019","key02":"125.63"},{"key01":"2020","key02":"174.8"},{"key01":"2018","key02":"68.05"},{"key01":"2021","key02":"279.7"}]}
	const name = ['财务到款'];
	renderLineChart('chart1', resp, name, '年')
	renderBarLineChart('chart5', resp, name)
	renderBarLineChart('chart6', resp, name)
}

function renderMonth() {
	// $.get(`${API_URL}getdata`, {
	// 	...rateparams,
	// 	xm01: fullTaskNumber,
	// 	chartNo: 3
	// }, function(res) {
	// 	const resp = eval("(" + res + ")").items[0].charts[0];
	// 	const name = ['部门到款'];
	// 	renderLineChart('chart2', resp, name, '月')
	// })
	
	const resp = {"chart_no":"3","chart_name":"收入-部门月度到款{传参数：xm01-任务编号；xm02-年份;返回值：key01-月份；key02-金额}","key_count":"2","data":[{"key01":"01","key02":"0"},{"key01":"02","key02":"-.24"},{"key01":"03","key02":".02"},{"key01":"04","key02":"18.82"},{"key01":"05","key02":"20.12"},{"key01":"06","key02":"23.33"},{"key01":"07","key02":"15.81"},{"key01":"08","key02":"61.16"},{"key01":"09","key02":"43.03"},{"key01":"10","key02":"138.41"},{"key01":"11","key02":"143.73"},{"key01":"12","key02":"573.05"}]}
	const name = ['部门到款'];
	renderLineChart('chart2', resp, name, '月')
}

function renderUnitReceive() {
	// $.get(`${API_URL}getdata`, {
	// 	...rateparams,
	// 	xm01: fullTaskNumber,
	// 	chartNo: 4
	// }, function(res) {
	// 	const resp = eval("(" + res + ")").items[0].charts[0];
	// 	const name = chart_name(resp.chart_name, ' ').splice(2);
	// 	renderPieChart('chart3', resp, name)
	// 	let items = '';
	// 	for (const item of resp.data) {
	// 		items += `<div class="item">
	// 									<p>${item.key01}</p>
	// 									<h4>${item.key03}%</h4>
	// 								</div>`
	// 	}
	// 	$('.container .chart-bar .bars').html(items)
	// 	renderTop5(resp.data[0].key01)
	// })
	const resp = {"chart_no":"4","chart_name":"收入-单位到款{参数：xm01-任务编号;返回值：key01-类型；key02-金额；key03-百分比}","key_count":"3","data":[{"key01":"院内单位","key02":"4.2866","key03":"27.6"},{"key01":"院外集团内","key02":"11.2433","key03":"72.4"}]}
	const name = chart_name(resp.chart_name, ' ').splice(2);
	renderPieChart('chart3', resp, name)
	let items = '';
	for (const item of resp.data) {
		items += `<div class="item">
									<p>${item.key01}</p>
									<h4>${item.key03}%</h4>
								</div>`
	}
	$('.container .chart-bar .bars').html(items)
	renderTop5(resp.data[0].key01)
}

function renderTop5(type) {
	$('.container .wrapper .table-chart .title h3.topTitle').text(type + '到款top5')
	// $.get(`${API_URL}getdata`, {
	// 	...rateparams,
	// 	xm01: fullTaskNumber,
	// 	xm02: type,
	// 	chartNo: 5
	// }, function(res) {
	// 	const resp = eval("(" + res + ")").items[0].charts[0];
	// 	const name = chart_name(resp.chart_name, ' ').splice(2);
	// 	getWlTopPay('chart4', resp)

	// })
	
	if(type==='院内单位'){
		const resp = {"chart_no":"5","chart_name":"收入-单位到款top5{参数：xm01-任务编号；xm02-单位类型；返回值：key01-单位名称；key02-金额}","key_count":"2","data":[{"key01":"北京宇航系统工程研究所","key02":"1.9372"},{"key01":"中国运载火箭技术研究院","key02":"1.0953"},{"key01":"首都航天机械有限公司","key02":"1.0738"},{"key01":"北京航天发射技术研究所","key02":".1803"}]}
		const name = chart_name(resp.chart_name, ' ').splice(2);
		getWlTopPay('chart4', resp)
	}else{
		const resp = {"chart_no":"5","chart_name":"收入-单位到款top5{参数：xm01-任务编号；xm02-单位类型；返回值：key01-单位名称；key02-金额}","key_count":"2","data":[{"key01":"北京航天长征飞行器研究所","key02":"11.2433"}]}
		const name = chart_name(resp.chart_name, ' ').splice(2);
		getWlTopPay('chart4', resp)
	}
}


function renderLineChart(elId, data, legends, unit) {
	noData(elId, data)
	let colors = ['#165DFF', '#14C9C9', '#F7BA1E']
	const legendData = legends
	let xData = [];
	for (let i = 0; i < data.data.length; i++) {
		xData.push(+data.data[i].key01);
	}
	const seriesData = [{
		name: '',
		type: "line",
		symbol: 'none',
		smooth: true,
		areaStyle: {
			color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
				offset: 0,
				color: 'rgba(20, 201, 201, 0.20)' // 0% 处的颜色
			}, {
				offset: 1,
				color: 'rgba(29,63,120,0)' // 100% 处的颜色
			}], false),
		},
		itemStyle: {
			color: colors[1],
		},
		smooth: true,
		data: [],
	}];
	for (const item of data.data) {
		seriesData[0].data.push(parseFloat(item['key02']))
	}
	have(elId) ? lineChartkzks(elId, seriesData, legendData, xData, unit, '亿元') : "";
}


function renderPieChart(elId, data) {
	noData(elId, data)
	const seriesData = [{
		name: '',
		type: 'pie',
		radius: '70%',
		itemStyle: {
			borderColor: '#fff',
			borderWidth: 2
		},
		labelLine: {
			show: false
		},
		label: {
			show: false
		},
		data: []
	}]
	for (const item of data.data) {
		seriesData[0].data.push({
			value: item.key02,
			name: item.key01
		})
	}

	have(elId) ? pieChart1(elId, seriesData, function(item) {
		handleClickItem(item.data.name)
	}) : ''
}
$('.container ').on('click', '.chart-bar .bars .item', function() {
	handleClickItem($(this).find('p').text())
})

function handleClickItem(name) {
	renderTop5(name)
}

// top5
function getWlTopPay(elId, data) {
	noData(elId, data)
	let yData = [];
	let xData = [];
	let legendData = ["到款金额"];
	const num = data.data.length > 5 ? 5 : data.data.length
	for (let i = 0; i < num; i++) {
		yData.push(data.data[i].key01);
		xData.push(data.data[i].key02);
	}

	let Data = [{
		name: "到款金额",
		type: "bar",
		barWidth: "20",
		barGap: 0,
		itemStyle: {
			color: "#722ED1",
		},
		data: xData,
	}];
	have(elId) ?
		horizontalBar(elId, legendData, yData, Data, "亿元", 200) :
		"";
}

function renderBarLineChart(elId, data, legends) {
	noData(elId, data)
	let colors = ['#B7F4EC', '#14C9C9', '#07828B']
	const legendData = legends
	let xData = [];
	for (let i = 0; i < data.data.length; i++) {
		xData.push(data.data[i].key01);
	}
	const Data = [];
	for (let i = 0; i < legendData.length; i++) {
		Data.push({
			name: legendData[i],
			type: legendData[i].indexOf('进度') > -1 || legendData[i].indexOf('率') > -1 ? "line" : "bar",
			barWidth: '30%',
			barGap: 0,
			yAxisIndex: legendData[i].indexOf('进度') > -1 || legendData[i].indexOf('率') > -1 ? 1 : 0,
			symbol: legendData[i].indexOf('进度') > -1 || legendData[i].indexOf('率') > -1 ? 'none' : 'rect',
			itemStyle: {
				color: legendData.length > 1 ? colors[i] : colors[1],
			},
			data: [],
		})
		for (const item of data.data) {
			Data[i].data.push(item['key0' + (i + 2)])
		}
	}
	have(elId) ? mulitYBar1(elId, xData, legendData, Data, '', '万元', '%', true, (legendData
		.length > 1)) : "";
}


function getTableData() {
	const resp = {
		data: [{
			key01: "安全生产处",
			key02: "100000",
			key03: "95000",
			key04: ".95",
			key05: "67000",
			key06: "62000",
			key07: "2015年"
		}, {
			key01: "一事业部",
			key02: "54000",
			key03: "69000",
			key04: "1.278",
			key05: "70000",
			key06: "33000",
			key07: "2016年"
		}, {
			key01: "科技处",
			key02: "50000",
			key03: "20000",
			key04: ".4",
			key05: "70000",
			key06: "60000",
			key07: "2017年"
		}, {
			key01: "行政处",
			key02: "25000",
			key03: "21000",
			key04: ".84",
			key05: "22000",
			key06: "21000",
			key07: "2018年"
		}, {
			key01: "四事业部",
			key02: "25000",
			key03: "72000",
			key04: "2.88",
			key05: "72000",
			key06: "43000",
			key07: "2019年"
		}, {
			key01: "三事业部",
			key02: "55000",
			key03: "80000",
			key04: "1.455",
			key05: "73000",
			key06: "47000",
			key07: "2020年"
		}, {
			key01: "工会办公室",
			key02: "46000",
			key03: "76000",
			key04: "1.652",
			key05: "57000",
			key06: "35000",
			key07: "2021年"
		}, {
			key01: "研究发展处",
			key02: "100000",
			key03: "90000",
			key04: ".9",
			key05: "64000",
			key06: "62000",
			key07: "2022年"
		}, {
			key01: "二事业部",
			key02: "71000",
			key03: "28000",
			key04: ".394",
			key05: "41000",
			key06: "65000",
			key07: "2023年"
		}]
	}
	renderTable1(resp.data)
	// renderTable2(resp.data)
	// renderTable3(resp.data)
}

function renderTable1(data) {
	const columns = [
		[{
				field: 'key01',
				title: '业务板块',
				align: 'left',
				rowspan: 2,
				valign: 'middle',
				formatter: blodText,
				footerFormatter: total
			},
			{
				field: 'key02',
				title: '预计到款',
				rowspan: 2,
				align: 'right',
				valign: 'middle',
				formatter: wyText,
				footerFormatter: sum
			},
			{
				title: '1-9月实际到款',
				colspan: 3,
				align: 'center',
				valign: 'middle',
			},
			{
				field: 'key04',
				title: '序时进度',
				align: 'right',
				rowspan: 2,
				valign: 'middle',
				formatter: wyText,
				footerFormatter: sum
			}
		],
		[{
				field: 'key02',
				title: '汇票到款',
				align: 'right',
				formatter: wyText,
				footerFormatter: sum
			},
			{
				field: 'key02',
				title: '电汇',
				align: 'right',
				formatter: wyText,
				footerFormatter: sum
			},
			{
				field: 'key02',
				title: '总计',
				align: 'right',
				formatter: wyText,
				footerFormatter: sum
			}
		]
	]
	renderTable('table1', data, columns, true)
	columns[0][0].title = '部门名称'
	renderTable('table2', data, columns, true)
}


let page = 1;
let limit = 10;
let searchVal = '';

function renderVoucherData() {
	// $.get(`${API_URL}getdata`, {
	// 	...rateparams,
	// 	xm01: fullTaskNumber,
	// 	xm02: fullYear,
	// 	pageRowCount: limit,
	// 	pageNum: page,
	// 	chartNo: 6
	// }, function(res) {
	// 	const resp = eval("(" + res + ")").items[0].charts[0];
	// 	recordTableDrawing(resp.data) // 数据传到 table组件
	// 	pagesDrawing(resp.data) // 数据传到 分页组件

	// })
	const resp = {"chart_no":"6","chart_name":"收入-到款凭证明细{参数：pageNum、pageRowCount,xm01-任务编号，xm02-年份；返回值：}","key_count":"13","data":[{"key01":"2022","key02":"04","key03":"26","key04":"00688","key05":"2304","key06":"拨入科研费","key07":"收入","key08":"029007032170102915050995","key09":"贷","key10":"868623.29318957354","key11":"F35220123","key12":"五室","key13":"1362"},{"key01":"2022","key02":"10","key03":"29","key04":"01440","key05":"2304","key06":"拨入科研费","key07":"收入","key08":"029007032170102715074505","key09":"贷","key10":"172102.125","key11":"L220-07-00250-Z","key12":"七事业部","key13":"1362"},{"key01":"2022","key02":"10","key03":"29","key04":"01440","key05":"2304","key06":"拨入科研费","key07":"收入","key08":"029007032170102715074505","key09":"贷","key10":"172102.125","key11":"L220-07-00250-Z","key12":"七事业部","key13":"1362"},{"key01":"2022","key02":"11","key03":"1","key04":"00099","key05":"2304","key06":"拨入科研费","key07":"收入","key08":"029007032170102715074505","key09":"贷","key10":"3612400","key11":"","key12":"七事业部","key13":"1362"},{"key01":"2022","key02":"11","key03":"8","key04":"00459","key05":"2304","key06":"拨入科研费","key07":"收入","key08":"029007032170102715074101","key09":"贷","key10":"10875573.54","key11":"L212-07-00568-Z","key12":"七事业部","key13":"1362"},{"key01":"2022","key02":"11","key03":"8","key04":"00459","key05":"2304","key06":"拨入科研费","key07":"收入","key08":"029007032170102715074101","key09":"贷","key10":"28242.4","key11":"L220-07-00243-Z","key12":"七事业部","key13":"1362"},{"key01":"2022","key02":"11","key03":"8","key04":"00459","key05":"2304","key06":"拨入科研费","key07":"收入","key08":"029007032170102715074101","key09":"贷","key10":"112969.6","key11":"L220-07-00243-Z","key12":"七事业部","key13":"1362"},{"key01":"2022","key02":"11","key03":"8","key04":"00459","key05":"2304","key06":"拨入科研费","key07":"收入","key08":"029007032170102715074101","key09":"贷","key10":"210052.85","key11":"L220-07-00249-Z","key12":"七事业部","key13":"1362"},{"key01":"2022","key02":"11","key03":"8","key04":"00459","key05":"2304","key06":"拨入科研费","key07":"收入","key08":"029007032170102715074101","key09":"贷","key10":"35303","key11":"L209-007-00823-Z","key12":"七事业部","key13":"1362"},{"key01":"2022","key02":"11","key03":"8","key04":"00459","key05":"2304","key06":"拨入科研费","key07":"收入","key08":"029007032170102715074505","key09":"贷","key10":"28275937.85","key11":"L220-07-00314-Z","key12":"七事业部","key13":"1362"}]}
	recordTableDrawing(resp.data) // 数据传到 table组件
	pagesDrawing(resp.data) // 数据传到 分页组件
}

let recoldCols = [
	[{
			field: 'key01',
			title: '年',
			width: 80
		},
		{
			field: 'key02',
			title: '月',
			width: 50
		},
		{
			field: 'key03',
			title: '日 ',
			width: 50
		},
		{
			field: 'key04',
			title: '凭证编号',
			templet: function(d) {
				return `<h6>${d.key04}</h6>`
			}
		},
		{
			field: 'key05',
			title: '科目编号',
			templet: function(d) {
				return `<h6>${d.key05}</h6>`
			}
		},
		{
			field: 'key06',
			title: '科目名称',
		},
		{
			field: 'key07',
			title: '属性',
		},
		{
			field: 'key08',
			title: '事业科研项目编号',
			width: 240
		},
		{
			field: 'key09',
			title: '记账方向',
			width: 100
		},
		{
			field: 'key10',
			title: '凭证金额',
			templet: function(d) {
				return `<h6>${(+d.key10).toFixed(2)}</h6>`
			}
		},
		{
			field: 'key11',
			title: '出库单号/合同号',
			width: 200
		},
		{
			field: 'key12',
			title: '其他标签',
		}
	]
];


function recordTableDrawing(data) {
	layui.use('table', function() {
		let table = layui.table;
		table.render({
			elem: '#table3',
			cols: recoldCols,
			data: data, // 数据
			limit: limit, // 每页显示的条数
			height: 230,
		});
	})
}

function pagesDrawing(data) {
	layui.use('laypage', function() {
		let laypage = layui.laypage;
		laypage.render({
			elem: 'laypage', //注意，这里的 page 是 ID，不用加 # 号
			count: data.length > 0 ? [0].key13 : 0, //数据总数，从服务端得到
			theme: '#14C9C9',
			limit: limit, // 每页条数
			prev: '<em><</em>',
			next: '<em>></em>',
			layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
			jump: function(obj, first) {
				//首次不执行
				if (!first) {
					limit = obj.limit;
					numpage(obj.curr, obj.limit) // 分页点击传参 
				}
			}
		});
	})

};

function numpage(curr, page_count) { // 分页切换-数据请求
	$.get(`${API_URL}getdata`, {
		...rateparams,
		xm01: fullTaskNumber,
		xm02: fullYear,
		pageRowCount: page_count,
		pageNum: curr,
		chartNo: 6
	}, function(res) {
		const resp = eval("(" + res + ")").items[0].charts[0];
		recordTableDrawing(resp.data) // 数据传到 table组件
	})
};